package com.kris.dev3.Thread;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *  多线程卖票
 */
public class ExcutorClass extends Thread {

    static int ticket = 20;
    private String name;
    public ExcutorClass(String name){
        this.name = name;
    }

    public void  run(){
        synchronized (this.getClass()){
            System.out.println("synchorized");
            while(ticket>0){
                if (ticket>0){
                    ticket--;
                    System.out.println(name+"开始卖第"+ticket);
                } else {
                    System.out.println("票已经卖完了..");
                    return;
                }

                try {
                    sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args){
        ExecutorService service = Executors.newFixedThreadPool(4);
        ExcutorClass ex = new ExcutorClass("线程1");
        for (int i=1;i<4;i++){
            service.execute(ex);
        }


    }
}
